#!/bin/sh

### This script mounts a sharepoint of your choice for rsync backups of your home folder ###
### Created on 11/3/2009 by Andrew Caldwell ###

###########################
# DEFINE GLOBAL VARIABLES #
###########################

currentDate=`date +%m-%d-%y`				# Set the current date
mountPoint=""								# Where you would like to mount the shared drive
sharePoint=""								# The name of the shared drive you'd like to mount
fileServer=""								# Name of the fileserver containing the share you'd like to mount
exitCode=0									# Keeps track of the exit code of functions, 0 is no error
remUsername=""								# Username to access the share
remPassword=""								# Password to access the share
locUsername=`whoami`						# Local Username
locDirectory=""								# Source directory for backup
setPreferences=""							# Save the configuration settings for next time
rsyncSwitch=""								# options for rsync

####################
# DEFINE FUNCTIONS #
####################


function detectPrevBkp ()
{

	currentTime=`date "+%s"`								# get the time now (seconds):
	Time3DaysAgo=$(($currentTime - (3 * 86400)))			# get the time 3 days ago (seconds):	
	eval $(stat -s ~/.ACbackup.txt)							# get the file modification time
																
	
	if [ "$st_mtime" -lt "$Time3DaysAgo" ]; then
		touch ~/.ACBackup.txt
		readParameters
	else
		exit 0
	fi
}
		

function getParameters ()
{
	
	rm -f ~/Library/Preferences/com.acdesigntech.ACBackup.txt
	
	echo "Please enter the share point: "
	read sharePoint
	echo "Please enter the mount point: "
	read mountPoint
	echo "Please enter the name/address of the fileserver: "
	read fileServer
	echo "Please enter a username for this share: "
	read remUsername
	echo "Please enter a password for this share: "
	read remPassword
	echo "Please enter the directory you'd like to back up (default is your home directory): " 	#single word only plz. Script can't handle 
	read locDirectory																			#multiple arguments yet.

	if [ -z $locDirectory ]; then
		locDirectory="/Users/$locUsername"
	fi

	mkfile -n 1k ~/Library/Preferences/com.acdesigntech.ACBackup.txt
	
	if [ -e ~/Library/Preferences/com.acdesigntech.ACBackup.txt ]; then
	
		echo $sharePoint >> ~/Library/Preferences/com.acdesigntech.ACBackup.txt
		echo $mountPoint >> ~/Library/Preferences/com.acdesigntech.ACBackup.txt
		echo $fileServer >> ~/Library/Preferences/com.acdesigntech.ACBackup.txt
		echo $remUsername >> ~/Library/Preferences/com.acdesigntech.ACBackup.txt
		echo $remPassword >> ~/Library/Preferences/com.acdesigntech.ACBackup.txt
		echo $locDirectory >> ~/Library/Preferences/com.acdesigntech.ACBackup.txt
		logFile
	else 
		echo "Unable to create file ~/Library/Preferences/com.acdesigntech.ACBackup.txt. " 
		echo "Unable to save settings. Check permissions and try again. Exiting program."
		exitCode=3
	fi
	
	
}

function readParameters ()
{
	if [ ! -e ~/Library/Preferences/com.acdesigntech.ACBackup.txt ]; then
		getParameters
	else
	
		oldIFS=$IFS
		IFS=$'\n'
		cd ~/Library/Preferences
	
		configFile=($(cat com.acdesigntech.ACBackup.txt))
	
		IFS=$oldIFS
	
		echo "Parameters are as follows:\n"
		for i in {0..5}
		do
			echo ${configFile[i]}
		done
		echo "Accept these settings? (y/n)"
		read settingsAccept
	
		if [ -z $settingsAccept ] || [ $settingsAccept == "y" ] || [ $settingsAccept == "Y" ]; then
			sharePoint=${configFile[0]}
			mountPoint=${configFile[1]}
			fileServer=${configFile[2]}
			remUsername=${configFile[3]}
			remPassword=${configFile[4]}
			locDirectory=${configFile[5]}
			logFile
		else 
			getParameters
		fi
	fi
}

function logFile ()
{
	mkdir -p ~/Library/Logs/AC_BU_Logs	# create a directory to hold the logs if not already there
	
	if [ -e ~/Library/Logs/AC_BU_Logs ]; then
		date +%m-%d-%y >> ~/Library/Logs/AC_BU_Logs/$currentDate.log			# begin writing the log
		echo "\n" >> ~/Library/Logs/AC_BU_Logs/$currentDate.log
		date +%H:%M:%S >> ~/Library/Logs/AC_BU_Logs/$currentDate.log
		echo "\n" >> ~/Library/Logs/AC_BU_Logs/$currentDate.log
		echo "--------\n" >> ~/Library/Logs/AC_BU_Logs/$currentDate.log
		open ~/Library/Logs/AC_BU_Logs/$currentDate.log							# open the current log file
		mountNetworkVolume
	else
		echo "Directory ~/Library/Logs/AC_BU_Logs does not exist. Unable to create directory." >> ~/Library/Logs/AC_BU_Logs/$currentDate.log
		exho "Check permissions and try again. Exiting program." >> ~/Library/Logs/AC_BU_Logs/$currentDate.log
		exitCode=2
	fi
}

function mountNetworkVolume ()
{
	mkdir -p /Volumes/$mountPoint
	mount_afp afp://$remUsername:$remPassword@$fileServer/$sharePoint /Volumes/$mountPoint
	mountedVolume=`df | grep $mountPoint`

	if [ "$mountedVolume" != "" ]; then
		echo "Volume $sharePoint mounted successfully. Beginning rsync." >> ~/Library/Logs/AC_BU_Logs/$currentDate.log
		syncServices
	else
		echo "Unable to mount volume $sharePoint." >> ~/Library/Logs/AC_BU_Logs/$currentDate.log
		echo "Check the username, password, and server address and try again. Exiting program" >> ~/Library/Logs/AC_BU_Logs/$currentDate.log
		exitCode=1
	fi
}

function syncServices ()
{
	rsync -avE --exclude=*/Library/Caches* $locDirectory /Volumes/$mountPoint >> ~/Library/Logs/AC_BU_Logs/$currentDate.log

	echo "rsync finished. Ejecting network disk in 30 seconds" >> ~/Library/Logs/AC_BU_Logs/$currentDate.log
	say rsync finished. Ejecting network disk in 30 seconds
	sleep 30
	diskutil unmount /Volumes/$mountPoint
	mountedVolume=`df | grep $mountPoint`

	if [ "$mountedVolume" == "" ]; then
		echo ""
	else
		echo "Unable to eject $mountPoint. rsync successful. Try manually unmounting. Exiting program." >> ~/Library/Logs/AC_BU_Logs/$currentDate.log
		exitCode=4
	fi

}
	
######################
# BEGIN MAIN PROGRAM #
######################

if [ -e ~/.ACBackup.txt ]; then
	detectPrevBkp
else
	mkfile -n 1k ~/.ACBackup.txt
	readParameters			
fi


case $exitCode in											# Create a list of exit codes to prevent 
	1)
		echo "Error in function mountNetworkVolume()" >> ~/Library/Logs/AC_BU_Logs/$currentDate.log
		;;
	2) 
		echo "Error in function logFile()" >> ~/Library/Logs/AC_BU_Logs/$currentDate.log
		;;
	3) 
		echo "Error in function getParameters()" >> ~/Library/Logs/AC_BU_Logs/$currentDate.log
		;;
	4) 
		echo "Warning in function syncServices()" >> ~/Library/Logs/AC_BU_Logs/$currentDate.log
		;;
	0) echo "Program completed successfully" >> ~/Library/Logs/AC_BU_Logs/$currentDate.log
		;;
esac

exit $exitCode

